package com.sky.sd.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.netflix.discovery.converters.Auto;
import com.sky.sd.common.web.response.Result;
import com.sky.sd.system.bean.dto.MenuNode;
import com.sky.sd.system.bean.po.Menu;
import com.sky.sd.system.bean.po.User;
import com.sky.sd.system.bean.vo.*;
import com.sky.sd.system.enums.MenuType;
import com.sky.sd.system.service.IPermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <br/>功能:
 * <br/>版本: 1.0
 * <br/>开发人员: 弓振
 * <br/>创建日期: 2020/8/11 17:36
 * <br/>修改日期: 2020/8/11 17:36
 * <br/>修改列表:
 */
@RestController
@RequestMapping("/system/perm")
@Api(tags = "系统权限")
public class PermController {

    @Autowired
    private IPermService permService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "menuId", value = "菜单id", dataType = "string", paramType = "query", example = ""),
    })
    public Result listPermByMenuId(String menuId) {
        List<MenuNode> menuNodes = permService.listPermByMenuId(menuId);
        return Result.success(menuNodes);
    }

    @GetMapping("/check/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "权限编码", dataType = "string", paramType = "query", example = "",required = true)
    })
    @ApiOperation(value = "权限编码是否存在")
    public Result check(PermCheckVo vo) {
        int cnt = permService.count(Wrappers.<Menu>lambdaQuery().ne(StringUtils.isNotBlank(vo.getId()), Menu::getId, vo.getId())
                .eq(Menu::getCode, vo.getCode()).eq(Menu::getType, MenuType.BUTTON));
        return Result.success(cnt);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增权限")
    public Result addMenu(@RequestBody PermAddVo vo) {
        permService.add(vo);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改权限")
    public Result updateMenu(@RequestBody PermUpdateVo vo) {
        permService.update(vo);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除权限")
    public Result deleteMenu(@RequestBody DeleteVo vo) {
        permService.delete(vo);
        return Result.success();
    }
}
